package vtca.nmcuong.btlist;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class MainActivity extends ListActivity {

	PAdapter adapter = null;
	List<Point> myListItems = null;
	public int oldDF;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		
		setContentView(R.layout.activity_main);

		myListItems = new ArrayList<Point>();
		//create list here
		myListItems.add(new Point("123", "412", "Point 1", false));
		myListItems.add(new Point("23","645","Point 2",true));
		myListItems.add(new Point("13","22","Point 3",false));
		myListItems.add(new Point("12","42","Point 4",false));
		myListItems.add(new Point("163","4","Point 5",false));
		setView();


		final Button addPoint = (Button)findViewById(R.id.addPointa);
		addPoint.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				addPoint(v);

			}
		});

	}



	private void setView(){
		adapter = new PAdapter(this, R.layout.point_item, myListItems, del, setdf, getoldDF);

		//methode of ListActivity
		this.setListAdapter(adapter);

	}




	private Handler del =new Handler(new Callback() {

		public boolean handleMessage(Message msg) {
			removeFileFromList(msg.arg1);
			return true;
		}
	});

	private Handler setdf =new Handler(new Callback() {

		public boolean handleMessage(Message msg) {


			setDefaultPoint(msg.arg1, msg.arg2);
			return true;
		}
	});

	private Handler getoldDF =new Handler(new Callback() {

		public boolean handleMessage(Message msg) {
			oldDF = msg.arg1;
			return true;
		}
	});

	public void removeFileFromList(int position){
		//		PointDTO point = myListItems.get(position);
		myListItems.remove(position);
		//		myListItems.remove(point);
		//		adapter.remove(point);
		//methode of ListActivity
		setListAdapter(adapter);
	}

	public void setDefaultPoint(int position,int olddfpostion){
		Point setNewDf = myListItems.get(position);
		setNewDf.isDf = true;

		Point setOldDf = myListItems.get(olddfpostion);
		setOldDf.isDf = false;

		//		myListItems.add(bk);
		setListAdapter(adapter);
	}


	public void addPoint(View v){

		LayoutInflater inflater = LayoutInflater.from(this);
		View dialogview = inflater.inflate(R.layout.input, null);

		AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
		dialogbuilder.setTitle("Add point");
		dialogbuilder.setView(dialogview);
		final AlertDialog dialogDetails = dialogbuilder.create();
		dialogDetails.show();

		final EditText tdXi =(EditText)dialogDetails.findViewById(R.id.tdXInput);
		final EditText tdYi = (EditText)dialogDetails.findViewById(R.id.tdYInput);
		final EditText pNameI = (EditText)dialogDetails.findViewById(R.id.pNameInput);
		final CheckBox isDfI = (CheckBox)dialogDetails.findViewById(R.id.isDfInput);

		Button cancel = (Button)dialogDetails.findViewById(R.id.cancel);
		cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialogDetails.dismiss();
			}
		});

		Button addP= (Button)dialogDetails.findViewById(R.id.addP);
		addP.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				myListItems.add(new Point(tdXi.getText().toString(),tdYi.getText().toString(),pNameI.getText().toString(),isDfI.isChecked()));
				if(isDfI.isChecked()==true){
					Point setOldDf = myListItems.get(oldDF);
					setOldDf.isDf = false;
				}
				setListAdapter(adapter);
				dialogDetails.dismiss();
			}
		});

	}

	//	
	//	@Override
	//	public boolean onCreateOptionsMenu(Menu menu) {
	//		// Inflate the menu; this adds items to the action bar if it is present.
	//		getMenuInflater().inflate(R.menu.activity_main, menu);
	//		return true;
	//	}

}
